package com.yuneec.fly.utils;

import com.yuneec.fly.DroneApplication;
import com.yuneec.fly.models.ConnectResource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 * Created by jw.gu on 2016/6/27.
 */
public class ConnectResourceUtils {

    public static void saveParams(ConnectResource params) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File(DroneApplication.getApplication().getFilesDir(), "ConnectResource")));
            oos.writeObject(params);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void saveChannelConfigs(ArrayList<byte[]> channelConfigs) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File(DroneApplication.getApplication().getFilesDir(), "FlightModePassingParams_channelConfigs")));
            oos.writeObject(channelConfigs);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static ConnectResource readSavedPararms() {
        ConnectResource parmas = null;
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(new File(DroneApplication.getApplication().getFilesDir(), "ConnectResource")));
            parmas = (ConnectResource) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return parmas;
    }

    public static ArrayList<byte[]> readSavedChannelConfigs() {
        ArrayList<byte[]> channelConfigs = null;
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(new File(DroneApplication.getApplication().getFilesDir(), "FlightModePassingParams_channelConfigs")));
            channelConfigs = (ArrayList<byte[]>) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return channelConfigs;
    }
}
